
import UIKit

class XSUserAccount: NSObject, NSCoding {
    
    var access_token: String?
    var expires_in: String!
    var uid: String?
    var creat_time: NSDate?
    var screen_name: String?
    var avatar_large: String?
    var userLogin: Bool?
    var userAccount: XSUserAccount?
    
    required init?(coder aDecoder: NSCoder)
    {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeObjectForKey("expires_in") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        creat_time = aDecoder.decodeObjectForKey("creat_time") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        userLogin = aDecoder.decodeBoolForKey("userLogin")
        userAccount = aDecoder.decodeObjectForKey("userAccount") as? XSUserAccount
    }
    
    func encodeWithCoder(aCoder: NSCoder)
    {
        aCoder.encodeObject(self.access_token, forKey: "access_token")
        aCoder.encodeObject(self.expires_in, forKey: "expires_in")
        aCoder.encodeObject(self.uid, forKey: "uid")
        aCoder.encodeObject(self.creat_time, forKey: "creat_time")
        aCoder.encodeObject(self.screen_name, forKey: "screen_name")
        aCoder.encodeObject(self.avatar_large, forKey: "avatar_large")
        aCoder.encodeBool(self.userLogin!, forKey: "userLogin")
        aCoder.encodeObject(self.userAccount, forKey: "userAccount")
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String){
    }
    
    // MARK: - 加载用户数据
    class func loadAccountInfor(dict: [String: AnyObject]?)
    {
        self.setValuesForKeysWithDictionary(dict!)
    }
    
    // MARK: - 加载已保存的数据
    func loadUserAccount() -> XSUserAccount?
    {
        let expires = (expires_in as NSString).doubleValue
        let creatTime = NSDate(timeIntervalSinceNow: expires)
        
        if userAccount == nil
        {
            let path =  (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.plist")
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XSUserAccount
            return (NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? XSUserAccount)!
        }
        else if userAccount != nil && creatTime .compare(creat_time!) == NSComparisonResult.OrderedDescending
        {
            return userAccount!
        }
        else
        {
            return nil
        }
    }
    
    // MARK: - 保存用户数据
    func saveUserAccount()
    {
        creat_time = NSDate()
        userLogin = true
        
        let path =  (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
}
